# -*- coding: utf-8 -*-#
#-------------------------------------------------------------------------------
# Name:         demo03
# Description:  
# Author:       Administrator
# Date:         2020/2/8
#-------------------------------------------------------------------------------

from selenium import webdriver
import time

class Demo01:

	def __init__(self):

		self.driver = webdriver.Firefox()
		self.driver.implicitly_wait(10)
		self.driver.maximize_window()
		self.driver.get('http://192.168.1.4:8080/WoniuSales/')
		self.driver.add_cookie({'name': 'username', 'value': 'admin'})
		self.driver.add_cookie({'name': 'password', 'value': 'admin'})
		self.driver.get('http://192.168.1.4:8080/WoniuSales')

	# 对页面内容进行截图操作
	def test_img(self):
		driver = self.driver

		# driver.find_element_by_id('submit').click()
		# 格式化时间
		ctime = time.strftime('%Y-%m-%d_%H-%M-%S',time.localtime())
		time.sleep(2)
		driver.get_screenshot_as_file(r'error_img\error_%s.png'%(ctime))
		# print(driver.get_screenshot_as_base64())
		# print(driver.get_screenshot_as_png())

	# 鼠标操作：对driver进行功能增强，使得driver具有双击、右键、悬停等功能
	def test_mouse(self):
		driver = self.driver
		driver.get('http://www.baidu.com')
		time.sleep(2)
		ele1 = driver.find_element_by_link_text('新闻')
		ele2 = driver.find_element_by_link_text('设置')
		from selenium.webdriver import ActionChains
		# 将增强的driver使用相应的方法对元素执行操作
		ActionChains(driver).context_click(ele1).perform()
		time.sleep(2)
		ele1.send_keys('W')
		# ActionChains(driver).move_to_element(ele2).perform()



	def test_element(self):
		driver = self.driver
		# uname = driver.find_element_by_tag_name('input')
		# uname.click()
		# uname.clear()
		# uname.send_keys('admin')

		eles = driver.find_elements_by_tag_name('input')
		driver.find_element_by_partial_link_text('公告管理')

	def do_login(self,username,upassword):
		#生成一个局部的浏览器驱动对象
		driver = self.driver

		uname = driver.find_element_by_id('username')
		uname.click()
		uname.clear()
		uname.send_keys(username)

		upass = driver.find_element_by_id('password')
		upass.click()
		upass.clear()
		upass.send_keys(upassword)

		driver.find_element_by_id('savelogin').click()

		driver.find_element_by_id('login').click()

		# xpath = xml + path
		# driver.find_element_by_xpath('/html/body/div[4]/div/form/div[6]/button').click()
	#
	def test_xpath(self):
		driver =self.driver
		driver.switch_to.parent_frame()
		# phone = driver.find_element_by_xpath('//input[@data-provide="typeahead"]')
		# phone = driver.find_element_by_xpath('//form[@class="form-inline"]/div[2]/input')
		phone = driver.find_element_by_xpath('//div[@id="vipsell"]/div/form/div[2]/input')
		# phone = driver.find_element_by_xpath('//input[@id="customerphone"]//../button')
		phone.click()
		phone.clear()
		phone.send_keys('12345678901')

	# 对弹出窗口的处理
	def test_alert(self):
		driver = self.driver
		driver.find_element_by_id('submit').click()
		time.sleep(2)
		# 从页面切换至弹出窗口
		print(driver.switch_to.alert.text)
		# 确定
		driver.switch_to.alert.accept()
		time.sleep(2)
		print(driver.switch_to.alert.text)
		driver.switch_to.alert.accept()
		# 取消
		# driver.switch_to.alert.dismiss()
		# 如果是输入框
		# driver.switch_to.alert.send_keys('hello')

	# 对windows窗口切换的处理
	def test_swtich_window(self):
		driver = self.driver
		driver.find_element_by_partial_link_text('测试').click()
		# windows窗口的标识是handle句柄
		# print(driver.current_window_handle)
		# 获取所有窗口的句柄
		window_handles = driver.window_handles
		driver.switch_to.window(driver.window_handles[-1])
		time.sleep(5)
		driver.find_element_by_link_text('关于蜗牛').click()
		time.sleep(2)
		window_handles = driver.window_handles
		print(window_handles)
		time.sleep(3)
		driver.switch_to.window(window_handles[0])
		driver.find_element_by_link_text('就业培训').click()

	# 对iframe窗口的切换
	def test_switch_iframe(self):
		driver = self.driver
		driver.find_element_by_partial_link_text('公告管理').click()
		headline = driver.find_element_by_id('headline')
		headline.click()
		headline.clear()
		headline.send_keys('今天元宵节')

		# driver.find_element_by_class_name('ke-iframe').send_keys('节日快乐')
		# time.sleep(2)
		# driver.find_element_by_id('add').click()

		# 修改公告的内容
		# time.sleep(2)
		# driver.find_element_by_xpath('/html/body/table/tbody/tr[2]/td[2]/div/div[2]/table[2]/tbody/tr[1]/td[5]/label[1]').click()
		# time.sleep(2)
		# content = driver.find_element_by_class_name('ke-iframe')
		# content.click()
		# content.clear()
		# content.send_keys('2020年春节结束')

		# frame参数中传递iframe元素对象
		iframe_ele = driver.find_element_by_class_name('ke-iframe')
		driver.switch_to.frame(iframe_ele)
		time.sleep(2)
		# driver已经切换到内部的iframe页面中
		driver.find_element_by_xpath('/html').send_keys('hello')
		driver.switch_to.default_content()
		driver.find_element_by_id('add').click()

	# 使用cookie绕过登录验证码
	def test_cookie(self):
		driver = webdriver.Firefox()
		driver.maximize_window()
		driver.implicitly_wait(10)
		driver.get('http://192.168.1.4:8080/WoniuSales')
		# 通过字典方式传递cookie信息
		driver.add_cookie({'name':'username','value':'admin'})
		driver.add_cookie({'name':'password','value':'admin'})
		driver.get('http://192.168.1.4:8080/WoniuSales')

	# 上传文件
	def test_uploadfile(self):
		driver = self.driver
		driver.find_element_by_link_text('批次管理').click()
		driver.find_element_by_id('batchfile').send_keys(r'F:\trans\直播中途休息时展示.txt')
		time.sleep(3)
		driver.find_element_by_css_selector('form.form-inline:nth-child(2) > div:nth-child(1) > input:nth-child(1)').click()


if __name__ == '__main__':

    d = Demo01()
    # d.do_login('admin','admin')
    # d.test_alert()
    # d.test_swtich_window()
    # d.do_login('admin','admin')
    # d.test_switch_iframe()
    # d.test_cookie()
    # d.test_uploadfile()
    # d.test_mouse()
    d.test_img()